class_name WeaponC_StateManager
extends WeaponC

@export var fsm:FSM
#region ExpressionProperty
const expression_property_name_dict:Dictionary = {
	attack = "attack", # value = 0 - 攻击键空闲,1 - 攻击键按下,2 - 攻击键保持按下,3 - 攻击键松开
	cur_count = "cur_count", # value = Int - 剩余的容量
}
#endregion

func get_animation(anim_name:String) -> Animation:
	var anim:Animation = weapon.animation_player.get_animation(anim_name)
	return anim if anim else null
func play_animation(anim_name:String,speed:float = 1):
	weapon.animation_player.play(anim_name,-1,speed)
func set_animation_speed_scale(scale:float):
	weapon.animation_player.speed_scale *= scale
func add_anim_finished_event(anim_name:String,callback:Callable,connect_flag:ConnectFlags = 0):
	var anim:Animation = weapon.animation_player.get_animation(anim_name)
	if anim and not weapon.animation_player.animation_finished.is_connected(callback):
		weapon.animation_player.animation_finished.connect(callback,connect_flag)
func remove_anim_finished_event(anim_name:String,callback:Callable):
	var anim:Animation = weapon.animation_player.get_animation(anim_name)
	if anim and weapon.animation_player.animation_finished.is_connected(callback):
		weapon.animation_player.animation_finished.disconnect(callback)
func translate_state(state_name:String):
	fsm.translate_state_by_state_name(state_name)
func set_fsm_expression_property(name:StringName,value):
	fsm.set_expression_property(name,value)

func init_component(weapon:Weapon):
	super.init_component(weapon)
	var c_capacity:WeaponC_Capacity = weapon.get_component_by_name("WeaponC_Capacity")
	if c_capacity:
		c_capacity.on_cur_count_changed.connect(func (cur_count):
			set_fsm_expression_property(expression_property_name_dict.cur_count,cur_count)
			)
	fsm.init_fsm(self)
func equip():
	super.equip()
	fsm.enable = true
	set_fsm_expression_property(expression_property_name_dict.attack,GameEnum.KeyActionState.Idle)
func unequip():
	fsm.enable = false
	set_fsm_expression_property(expression_property_name_dict.attack,GameEnum.KeyActionState.Idle)
	super.unequip()
func discard():
	set_fsm_expression_property(expression_property_name_dict.attack,GameEnum.KeyActionState.Idle)
	pass

func _init() -> void:
	weapon_component_name = "WeaponC_StateManager"
	priority = 100
func _ready() -> void:
	set_fsm_expression_property(expression_property_name_dict.attack,GameEnum.KeyActionState.Idle)
	set_fsm_expression_property(expression_property_name_dict.cur_count,weapon.get_component_by_name("WeaponC_Capacity").cur_count)
